package com.example.back.mapper.otd;

import com.example.back.entity.otd.Posts;
import com.example.back.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class PostsSqlProvider01 {
    public String getPostsAndUserByText(Posts  posts) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT p.id, p.title, p.time, p.text, p.url, p.browse, p.userId, ");
        sql.append("u.yhm, u.nc, u.tx ");
        sql.append("FROM post p ");
        sql.append("JOIN user u ON p.userId = u.id ");
        sql.append("WHERE 1=1 ");

        List<String> conditions = new ArrayList<>();
        if (StringUtil.isNotEmpty(posts.getTitle())) {
            conditions.add("p.title LIKE concat('%',#{title},'%')");
        }
        if (StringUtil.isNotEmpty(posts.getText())) {
            conditions.add("p.text LIKE concat('%',#{text},'%')");
        }
        if (StringUtil.isNotEmpty(posts.getTime())) {
            conditions.add("p.time LIKE concat('%',#{time},'%')");
        }

        if (!conditions.isEmpty()) {
            sql.append("AND (").append(String.join(" OR ", conditions)).append(")");
        }
        return sql.toString();
    }

}
